package com.nulldev.util.web.nCaptcha;

import java.io.InputStream;
import java.util.HashMap;
import java.util.regex.Pattern;

import com.nulldev.util.io.FileUtil;
import com.nulldev.util.java.DataSet;
import com.nulldev.util.other.ArrayUtils;
import com.nulldev.util.uri.server.HttpServer;
import com.nulldev.util.uri.server.RequestType;
import com.nulldev.util.uri.server.v2.HttpPage2;
import com.nulldev.util.uri.server.v2.HttpServer2;
import com.nulldev.util.web.nCaptcha.backend.Captcha;
import com.nulldev.util.web.nCaptcha.backend.CaptchaPage;
import com.nulldev.util.web.nCaptcha.backend.CheckCaptcha;
import com.nulldev.util.web.nCaptcha.backend.RemoveCaptchaState;
import com.nulldev.util.web.nCaptcha.backend.RequestCaptcha;
import com.nulldev.util.web.nCaptcha.backend.RequestCaptchaNS;
import com.nulldev.util.web.nCaptcha.backend.ValidUID;
import com.nulldev.util.web.nCaptcha.captchas.advanced.MultiWordCheck;
import com.nulldev.util.web.nCaptcha.captchas.auto.AutoPayloadSubmission;
import com.nulldev.util.web.nCaptcha.captchas.auto.RequestAutoCaptchaPayload;
import com.nulldev.util.web.nCaptcha.captchas.identifcation.messedwords.MessedUpWordsCaptcha;
import com.nulldev.util.web.nCaptcha.captchas.obftext.ImageTextCaptcha;
import com.nulldev.util.web.nCaptcha.captchas.obftext.MathCaptcha;
import com.nulldev.util.web.nCaptcha.captchas.obftext.TrueFalseStatement;
import com.nulldev.util.web.nCaptcha.captchas.shapes.ShapeIdentification;

public class nCaptcha {
	public static final Captcha[] CAPTCHAS = {
			new ImageTextCaptcha(),
			new ShapeIdentification(),
			new MathCaptcha(),
			new TrueFalseStatement(),
			new MultiWordCheck(),
			new MessedUpWordsCaptcha(),
	};

	public static Captcha randomCaptcha() {
		return ArrayUtils.random(CAPTCHAS);
	}
	
	public static void loadCaptchaSystem(final HttpServer server) {
		server.addPage(new RequestCaptcha());
		server.addPage(new RequestCaptchaNS());
		server.addPage(new CheckCaptcha());
		server.addPage(new ValidUID());
		server.addPage(new RemoveCaptchaState());
	}
	
	public static void loadCaptchaSystem(final HttpServer2 server) {
		server.addPage(new RequestCaptcha());
		server.addPage(new RequestCaptchaNS());
		server.addPage(new CheckCaptcha());
		server.addPage(new ValidUID());
		server.addPage(new RemoveCaptchaState());
		server.addPage(new RequestAutoCaptchaPayload());
		server.addPage(new CaptchaPage());
		server.addPage(new AutoPayloadSubmission());
		server.addPage(new HttpPage2("/xxtea.js") {
			
			final byte[] data = FileUtil.getResourceBytes(Captcha.class, "xxtea.js");
			
			@Override
			public DataSet<Integer, InputStream> request(RequestType type, String url, HashMap<String, String> headers,
					byte[] extraData, String ip) {
				return new DataSet<Integer, InputStream>(200, createStream(data));
			}
		});
		server.addPage(new HttpPage2("/sha256.js") {
			
			final byte[] data = FileUtil.getResourceBytes(Captcha.class, "sha256.js");
			
			@Override
			public DataSet<Integer, InputStream> request(RequestType type, String url, HashMap<String, String> headers,
					byte[] extraData, String ip) {
				return new DataSet<Integer, InputStream>(200, createStream(data));
			}
		});
	}
	
	public static String translateFromType(final String ctype) {
		String type = ctype;
		if (type.startsWith("type:")) {
			type = type.replace(Pattern.quote("type:"), "");
		}
		if (type.equals("screenwords")) {
			return "Enter the letters above.";
		}else if (type.equals("shapes")) {
			return "Enter the shape above in LOWER case.";
		}else if (type.equals("math")) {
			return "Enter the answer to the math question.";
		}else if (type.equals("objectid")) {
			return "Enter the item in the above image.";
		}else if (type.equals("truefalse")) {
			return "Enter the value of the statement. [true/false]";
		}else if (type.equals("messedup")) {
			return "Enter the word within the halfed then reversed word above.";
		}else if (type.equals("multiword")) {
			return "Enter the words above.";
		}
		return type;
	}
}
